package cn.gudian.cms.System

import java.util.regex.Pattern

class SysConfigController {

  def index = { redirect(action: "list", params: params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  def list = {
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    [sysConfigInstanceList: SysConfig.list(params), sysConfigInstanceTotal: SysConfig.count()]
  }
  def config = {
    [sysConfigInstanceList: SysConfig.list()]
  }

  def create = {
    def sysConfigInstance = new SysConfig()
    sysConfigInstance.properties = params
    return [sysConfigInstance: sysConfigInstance]
  }

  def save = {
    def sysConfigInstance = new SysConfig(params)
    if (!sysConfigInstance.hasErrors() && sysConfigInstance.save()) {
      flash.message = "sysConfig.created"
      flash.args = [sysConfigInstance.id]
      flash.defaultMessage = "SysConfig ${sysConfigInstance.id} created"
      redirect(action: "show", id: sysConfigInstance.id)
    }
    else {
      render(view: "create", model: [sysConfigInstance: sysConfigInstance])
    }
  }

  def show = {
    def sysConfigInstance = SysConfig.get(params.id)
    if (!sysConfigInstance) {
      flash.message = "sysConfig.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "SysConfig not found with id ${params.id}"
      redirect(action: "list")
    }
    else {
      return [sysConfigInstance: sysConfigInstance]
    }
  }

  def edit = {
    def sysConfigInstance = SysConfig.get(params.id)
    if (!sysConfigInstance) {
      flash.message = "sysConfig.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "SysConfig not found with id ${params.id}"
      redirect(action: "list")
    }
    else {
      return [sysConfigInstance: sysConfigInstance]
    }
  }

  def update = {
    def sysConfigInstance = SysConfig.get(params.id)
    if (sysConfigInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (sysConfigInstance.version > version) {

          sysConfigInstance.errors.rejectValue("version", "sysConfig.optimistic.locking.failure", "Another user has updated this SysConfig while you were editing")
          render(view: "edit", model: [sysConfigInstance: sysConfigInstance])
          return
        }
      }
      sysConfigInstance.properties = params
      if (!sysConfigInstance.hasErrors() && sysConfigInstance.save()) {
        Sys.saveMap();
        flash.message = "sysConfig.updated"
        flash.args = [params.id]
        flash.defaultMessage = "SysConfig ${params.id} updated"
        redirect(action: "show", id: sysConfigInstance.id)
      }
      else {
        render(view: "edit", model: [sysConfigInstance: sysConfigInstance])
      }
    }
    else {
      flash.message = "sysConfig.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "SysConfig not found with id ${params.id}"
      redirect(action: "edit", id: params.id)
    }
  }

  def updateAll = {
    // println params
    def p = request.getParameterMap()
    p.each {
      def id
      if (isNumeric(it?.key)) {
        id = it?.key
      } else {
        id = "0";
      }
      def sysConfigInstance = SysConfig.get(Integer.parseInt(id))

      if (sysConfigInstance) {

        SysConfig.executeUpdate("update SysConfig b set b.varValue=? where b.id=?", [it?.value[0], sysConfigInstance?.id])

        flash.message = "修改成功"
      }

    }
    Sys.saveMap();
    redirect(action: "config")
  }

  static boolean isNumeric(String str) {
    Pattern pattern = Pattern.compile("[0-9]*");
    return pattern.matcher(str).matches();
  }

  def delete = {
    def sysConfigInstance = SysConfig.get(params.id)
    if (sysConfigInstance) {
      try {
        sysConfigInstance.delete()
        flash.message = "sysConfig.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "SysConfig ${params.id} deleted"
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "sysConfig.not.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "SysConfig ${params.id} could not be deleted"
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "sysConfig.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "SysConfig not found with id ${params.id}"
      redirect(action: "list")
    }
  }

  def deleteAll = {
    def qstr = params.qstr
    if (qstr) {
      try {
        SysConfig.executeUpdate("delete SysConfig where id in(" + qstr + ")")
        flash.message = "delete successfully:" + qstr
        redirect(action: "list")
      } catch (Exception e) {
        flash.message = "delete failed:" + qstr
        redirect(action: "list")
      }
    }
  }
}
